#include "SMARTStateEquationSolveLib.h"

void SMARTSESIterationSolve(double *A, double *B, double *C, double *D, SES_SRC *sesSrc, double **X, double *Y, 
	int n, double steps, double hfactor, FILE *fileHandler);
void SMARTSESUpadateUMatrix(double **UPoint, SES_SRC *sesSrc, int step, int n);
void SMARTSESCalculateY(double *C, double *X, double *D, double *U, double *Y, int n);
void SMARTSESSolveFormular(char **SparseMatrix, double *A, double **X, double *B, double *U, int n, double hfactor);
void SMARTSESConstructAXRHS(double *A, double *X, double *B, double *U, 
		double **RHS, double **AMatrix, int n, double hfactor);
void SMARTSESSolveAXRHS(double *AMatirx, double **X, double *RHS, int n, char **SparseMatrix);

/****************************Start of SPARSE  Part****************************************/
typedef  spREAL  RealNumber, *RealVector;
	
typedef  struct
{   RealNumber Real;
   RealNumber Imag;
}ComplexNumber, *ComplexVector;

static struct Bin {
    ComplexVector   Array;
    struct Bin     *Next;
}*FirstArray, *CurrentArray = NULL;

static ComplexVector CheckOutComplexArray(int Count);
int Init();

static int Init(RealNumber* pElement,char* pInitInfo,int Row,int Col)
{
    *pElement = *(RealNumber*)pInitInfo;    
    return 0;
}

static ComplexVector CheckOutComplexArray(int Count)
{
	struct Bin Bin, *pBin;
	ComplexVector Temp;
	/* Begin `CheckOutComplexArray'. */
    	if (CurrentArray == NULL || CurrentArray->Next == NULL)
   	{   	
		pBin = SMARTNEW(struct Bin, 1);
       	Temp = SMARTNEW(ComplexNumber, Count);
        	if (pBin == NULL || Temp == NULL)
        	{   
			fprintf( stderr, "Insufficient memory available.\n");
            		exit(1);
        	}
        	Bin.Array = Temp;
        	Bin.Next = NULL;
        	*pBin = Bin;
        	if (CurrentArray == NULL)
            		FirstArray = CurrentArray = pBin;
        	else
        	{   	
			CurrentArray->Next = pBin;
            		CurrentArray = pBin;
        	}
    	}
    	else
    	{   	
		pBin = CurrentArray;
        	CurrentArray = pBin->Next;
    	}    	
    	return pBin->Array;
}
/*****************************End of SPARSE Part****************************************/
